package ru.narod.amidoriya.app.sushibaramidoriya;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import android.util.Log;

public class ListBludo extends BaseAdapter {
	private Context mContext;
	private List<? extends Map<String, ?>> mData;	
	Bitmap appdri;
	int sizes;
	int msizes;
	String pic;
	Zakaz sqldbc_bludoinsert, sqldbc_bludocount;
	SQLiteDatabase sqldb_bludoinsert, sqldb_bludocount;
	MainActivity MA;
	final String LOG_TAG = "myLogs";
	
	public ListBludo(Context c, List<? extends Map<String, ?>> data,int sizes) {
		mContext = c;
		mData = data;
		msizes = sizes;
	}

	public int getCount() {
		return mData.size();
	}

	public Object getItem(int position) {
		return mData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int Countbludo(int position) {
		// �������������� ��� �����
     	sqldbc_bludocount = new Zakaz(mContext);
     	// ���� ������ � ������
     	sqldb_bludocount = sqldbc_bludocount.getWritableDatabase();
     	String selbl1 = "SELECT " + Zakaz.UID + ", "+ Zakaz.DNAMEDISH + ", "
     			 + Zakaz.DPRICE + " FROM " + Zakaz.dtable+
     			 " WHERE ("+ Zakaz.DNAMEDISH +" = '"+ mData.get(position).get("b_0").toString() + "') AND ("+
				Zakaz.DPRICE +" = '"+ mData.get(position).get("b_1").toString() + "') AND ("+ Zakaz.DVES +" = '"+ mData.get(position).get("b_3").toString() +"')";
     	
		Cursor cursorbl_bludocount = sqldb_bludocount.rawQuery(selbl1, null);
		
		Integer ctbl_bludocount =  cursorbl_bludocount.getCount();
		
		cursorbl_bludocount.close();
		
     	
		sqldb_bludocount.close();
	    sqldbc_bludocount.close();
	    //
	    
	    //
	    return ctbl_bludocount;
	}
	public void Insertbludo(int position) {
		try {
			sqldbc_bludoinsert = new Zakaz(mContext);
	     	// ���� ������ � ������
	     	sqldb_bludoinsert = sqldbc_bludoinsert.getWritableDatabase();
			String insertsqlite = "INSERT INTO " + 
				    Zakaz.dtable + 
				     " (" + Zakaz.DNAMEDISH + "," + Zakaz.DPRICE + "," + Zakaz.DDOSTAVKA +
				     "," + Zakaz.DVES + "," + Zakaz.DPIC + "," + Zakaz.DPICFULL + "," + Zakaz.DSOSTAV +
				     ") VALUES ('" + mData.get(position).get("b_0").toString() + "','" + mData.get(position).get("b_1").toString() + "','" + mData.get(position).get("b_2").toString() + 
				     "','" + mData.get(position).get("b_3").toString() + "','" + mData.get(position).get("b_4").toString() + "','" + mData.get(position).get("b_5").toString() + "','" + mData.get(position).get("b_6").toString() +
				     "')";
			
			sqldb_bludoinsert.execSQL(insertsqlite);
			
			sqldb_bludoinsert.close();
		    sqldbc_bludoinsert.close();
		    
		} catch (Exception e) {

		}	
	}
	public void viewbludocount(int position, View convertView) {
		View itemlistbl;
		ViewGroup parent = null;
		if (convertView == null) {
			itemlistbl = new View(mContext);
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
			itemlistbl = inflater.inflate(R.layout.itemlistbludo, parent, false);
		} else {
			itemlistbl = (View) convertView;
		}
		final TextView Icountbludo = (TextView) itemlistbl.findViewById(R.id.icountbludo);
		Insertbludo(position);
		try {
			if (Countbludo(position) > 0) {
			Icountbludo.setVisibility(View.VISIBLE);
			Icountbludo.setText("");
			Icountbludo.setText(Integer.valueOf(Countbludo(position)).toString());
			} else {
				Icountbludo.setVisibility(View.VISIBLE);
				Icountbludo.setText("");
				}
		}
		catch (Exception e) {
			Icountbludo.setVisibility(View.VISIBLE);
			Icountbludo.setText("");
			Icountbludo.setVisibility(View.INVISIBLE);
		}
		
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		View itemlistbl;

		if (convertView == null) {
			itemlistbl = new View(mContext);
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
			itemlistbl = inflater.inflate(R.layout.itemlistbludo, parent, false);
		} else {
			itemlistbl = (View) convertView;
		}


		ImageView Ipicsmallpludo = (ImageView) itemlistbl.findViewById(R.id.ipicsmallpludo);
		TextView Inamebludo = (TextView) itemlistbl.findViewById(R.id.inamebludo);
		TextView Ipricebludo = (TextView) itemlistbl.findViewById(R.id.ipricebludo);
		TextView Iweightpiudo = (TextView) itemlistbl.findViewById(R.id.iweightpiudo);
		TextView Idostavkabludo = (TextView) itemlistbl.findViewById(R.id.idostavkabludo);
		final TextView Icountbludo = (TextView) itemlistbl.findViewById(R.id.icountbludo);
		
		
		try {
			pic = mData.get(position).get("b_4").toString();
			if (pic == null) {
				pic = "";
			}
			if (pic.equals("")) {
				//Ipicsmallpludo.setImageResource(R.drawable.no_pic);
				Ipicsmallpludo.setImageURI(Uri.parse(""));
			} 
			else
			{
				
				
				File aboutpic = new File(mData.get(position).get("b_5").toString());
				InputStream isan = new FileInputStream(aboutpic);
				Bitmap bmSource =  BitmapFactory.decodeStream(isan);
				Bitmap bmThumbnail = ThumbnailUtils.extractThumbnail(bmSource,
						msizes, msizes);
				Ipicsmallpludo.setImageBitmap(bmThumbnail);
			}
			
		}
		catch (Exception e) {
			Ipicsmallpludo.setImageURI(Uri.parse(""));	
		}
//
		try {
			Inamebludo.setText(mData.get(position).get("b_0").toString());
		}
		catch (Exception e) {
			Inamebludo.setText("");
		}
//
		try {
			Ipricebludo.setText(mData.get(position).get("b_1").toString());
		}
		catch (Exception e) {
			Ipricebludo.setText("");
		}
//
		try {
			Idostavkabludo.setText(mData.get(position).get("b_2").toString());
		}
		catch (Exception e) {
			Idostavkabludo.setText("");
		}
//
		try {
			Iweightpiudo.setText(mData.get(position).get("b_3").toString());
		}
		catch (Exception e) {
			Iweightpiudo.setText("");
		}
		try {
			if (Countbludo(position) > 0) {
			Icountbludo.setVisibility(View.VISIBLE);
			Icountbludo.setText("");
			Icountbludo.setText(Integer.valueOf(Countbludo(position)).toString());
			} else {
				Icountbludo.setVisibility(View.VISIBLE);
				Icountbludo.setText("");
				}
		}
		catch (Exception e) {
			Icountbludo.setVisibility(View.VISIBLE);
			Icountbludo.setText("");
			Icountbludo.setVisibility(View.INVISIBLE);
		}
		
		
		return itemlistbl;
	}
	
	
}
